<div class="layui-fluid layui-anim febs-anim" id="febs-role" lay-title="标签管理">
    <div class="layui-row layui-col-space8 febs-container">
        <div class="layui-col-md7 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="role-table-form" id="role-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md9 layui-col-sm9 layui-col-xs9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">标签名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="roleName" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain  table-action"
                                     id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain  table-action"
                                     id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="role:add,user:delete,role:export">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="roleTable" lay-data="{id: 'roleTable'}"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md5 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header" id="form-header">新增标签</div>
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" action="" lay-filter="role-form">

                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">标签名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" autocomplete="off" class="layui-input" minlength="2"
                                       maxlength="10" lay-verify="range">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">标签颜色：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="bgcolor" value="" placeholder="请选择颜色" class="layui-input"
                                       id="test-form-input" lay-verify="remark" disabled="disabled">
                            </div>
                            <div class="layui-inline">
                                <div id="test-form"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">字体颜色：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="fontcolor" value="" placeholder="请选择颜色" class="layui-input"
                                       id="font-form-input" lay-verify="remark" disabled="disabled">
                            </div>
                            <div class="layui-inline">
                                <div id="font-form"></div>
                            </div>
                        </div>

                        <div class="layui-form-item febs-hide">
                            <button type="reset" class="layui-btn" id="reset-form"></button>
                            <button class="layui-btn" lay-submit="" lay-filter="role-form-submit"
                                    id="submit-form"></button>
                            <button class="layui-btn" lay-submit="" lay-filter="preview-form-submit"
                                    id="preview-form"></button>
                        </div>
                    </form>
                </div>

                <div class="layui-block febs-table-full">
                    <button type="button" class="layui-btn layui-btn-radius" id="btnPreview">效果按钮</button>
                </div>

                <div class="layui-card-footer">
                    <div>
                        <button type="button" class="layui-btn layui-btn-radius" id="preview">预览效果</button>
                        <button class="layui-btn" id="submit">保存</button>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<script type="text/html" id="role-option">
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>

<script type="text/html" id="bat">

    <!--    <span class="layui-badge" style="background-color: {{d.title }} ">{{d.title }}</span>-->
    <!--    <span style="color:{{d.bgcolor}} ">{{ d.sex }}</span>-->

    <!--templet: '<div><a href="/detail/{{d.id}}" class="layui-table-link">{{d.title}}</a></div>'-->


</script>

<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
<script data-th-inline="none" type="text/javascript">
    layui.use(['colorpicker', 'dropdown', 'jquery', 'laydate', 'febs', 'form', 'eleTree', 'table', 'validate'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            colorpicker = layui.colorpicker,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            validate = layui.validate,
            $view = $('#febs-role'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $preview = $view.find('#preview'),
            $btnPreview = $view.find('#btnPreview'),
            $submit = $view.find('#submit'),
            $searchForm = $view.find('#role-table-form'),
            $header = $view.find('#form-header'),
            tableIns;

        form.verify(validate);
        form.render();

        initTable();

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            , color: '#1c97f5'
            , done: function (color) {
                $('#test-form-input').val(color);
            }
        });

        colorpicker.render({
            elem: '#font-form'
            , color: '#1c97f5'
            , done: function (color) {
                $('#font-form-input').val(color);
            }
        });

        laydate.render({
            elem: '#createTime',
            range: true
        });

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                let checkStatus = table.checkStatus('roleTable');
                if (name === 'add') {
                    resetRoleForm();
                    febs.alert.info("请在表单中填写相关信息");
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的角色');
                    } else {
                        febs.modal.confirm('删除角色', '确定删除所选角色？', function () {
                            let roleIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                roleIds.push(item.roleId);
                            });
                            deleteRoles(roleIds.join(','));
                        });
                    }
                }

                if (name === 'export') {
                    let params = getQueryParams();
                    params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
                    params.pageNum = $view.find(".layui-laypage-em").next().html();
                    febs.download(ctx + 'role/excel', params, '角色信息表.xlsx');
                }
            },

            options: [{
                name: 'add',
                title: '新增角色',
                perms: 'role:add'
            }, {
                name: 'delete',
                title: '删除角色',
                perms: 'role:delete'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'role:export'
            }]
        });

        table.on('tool(roleTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'edit') {
                $header.text('修改角色');
                form.val("role-form", {
                    "roleId": data.roleId,
                    "roleName": data.roleName,
                    "remark": data.remark
                });
                if (data.menuIds) {
                    menuTree.setChecked(data.menuIds.split(','), true);
                } else {
                    menuTree.setChecked([], true);
                }
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除角色', '确定删除该角色？', function () {
                    deleteRoles(data);
                });
            }
        });

        $query.on('click', function () {
            resetRoleForm();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $preview.on('click', function () {
            $view.find('#preview-form').trigger('click');
        });

        $reset.on('click', function () {
            resetRoleForm();
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $submit.on('click', function (json) {
            $view.find('#submit-form').trigger('click');
        });

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            , color: '#1c97f5'
            , done: function (color) {
                $('#test-form-input').val(color);
            }
        });

        colorpicker.render({
            elem: '#font-form'
            , color: '#1c97f5'
            , done: function (color) {
                $('#font-form-input').val(color);
            }
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'roleTable',
                url: ctx + 'web/nursingclass',
                method: 'post',
                page: false,
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": "", //解析提示文本
                        data: res.data //解析数据列表
                    };
                },
                cols: [[
                    {type: 'checkbox'},
                    {field: 'title', title: '标签名称', minWidth: 120},
                    {title: '预览效果', templet:
                            function (d) {
                                return '<button  className = "layui-btn layui-btn-radius"  style="border:none;border-radius:3px;padding: 3px 3px 3px 3px;background-color: '+ d.bgcolor + ';color:' + d.fontcolor + '">' + d.title + '</button>'

                            }
                    },
                    {title: '操作', toolbar: '#role-option', width: 100}
                ]]
            });
        }

        function getQueryParams() {
            let params = $searchForm.serializeJson();
            params.invalidate_ie_cache = new Date();
            return params;
        }

        function resetRoleForm() {
            $view.find('#reset-form').trigger('click');
            $header.text('新增标签');
        }

        /**
         * 预览效果
         */
        form.on('submit(preview-form-submit)', function (data) {
            $btnPreview.css({'background-color': data.field.bgcolor, 'color': data.field.fontcolor});
            $btnPreview.text(data.field.title)
            return false;
        });

        /**
         * 保存提交
         */
        form.on('submit(role-form-submit)', function (data) {
            const json = JSON.stringify(data.field);
            console.log(json)
            febs.post("http://localhost:8080" + '/web/add/nursingclass', data.field, function () {
                febs.alert.success('新增成功');
                $query.trigger('click');
            });

            return false;
        });


        /**
         * 删除标签
         * @param roleIds
         */
        function deleteRoles(roleIds) {
            febs.post(ctx + 'web/delete/nursingclass', roleIds, function () {
                febs.alert.success('删除成功');
                $query.trigger('click');
            })
        }
    });
</script>